import { toDataURL } from 'qrcode'

declare module 'qrcode' {
	function toDataURL(text: string, callback: (error: Error | null, url: string) => void): void;
}

export default defineNuxtPlugin((nuxtApp) => {
	nuxtApp.vueApp.directive('qrcode', {
		mounted(el, binding) {
			if (process.client) {
				const img = document.createElement('img');
				toDataURL(binding.value, (error: Error | null, url: string) => {
					img.src = url;
					img.style.width = '100%';
					el.appendChild(img);
				});
			}
		}
	})

});